package com.yingpai.fitness.dvp_2_0.main.activity.shop;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.main.adapter.shop.CustomPagerAdapter;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.ShopSaleData;
import com.yingpai.fitness.dvp_2_0.main.fragment.shop.ShopSaleFragment;
import com.yingpai.fitness.dvp_2_0.main.widget.CustomViewPager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class ShopSaleActivity extends CommonActivity implements OnTabSelectListener {


    @BindView(R.id.shop_sale_sliding_tab)
    SlidingTabLayout shopSaleSlidingTab;
    @BindView(R.id.shop_sale_vp)
    CustomViewPager shopSaleVp;
    private CustomPagerAdapter mAdapter;
    private ArrayList<Fragment> mList = new ArrayList<>();
    private int type;
    private int brandId;
    private String content;
    private String title;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.shop_sale_activity);
    }

    @Override
    protected void initData() {
        showDialog(this, null);
        HttpParams httpParams = new HttpParams();
        httpParams.put("categoryId", 1);
        httpParams.put("pageNum", 1);
        httpParams.put("pageSize", 1);
        httpParams.put("type", type);
        httpParams.put("brandId", brandId);
        httpParams.put("cont", content);
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.SHOP_LIST, httpParams, Url.SHOP_LIST, new Callback<ShopSaleData>() {
            @Override
            public void onSuccess(ShopSaleData shopSaleData) {
                if (Constant.REQUEST_SUCCESS.equals(shopSaleData.getResult())) {
                    shopSaleVp.setOffscreenPageLimit(shopSaleData.getMap().getCategoryList().size());

                    String[] mTitlesTopTab = new String[shopSaleData.getMap().getCategoryList().size()];

                    for (int i = 0; i < shopSaleData.getMap().getCategoryList().size(); i++) {
                        mTitlesTopTab[i] = shopSaleData.getMap().getCategoryList().get(i).getCategoryName();
                        mList.add(ShopSaleFragment.getInstance(shopSaleData.getMap().getCategoryList().get(i).getId(), type, brandId, content));

                    }

                    mAdapter = new CustomPagerAdapter(getSupportFragmentManager(), mList, mTitlesTopTab);

                    shopSaleVp.setAdapter(mAdapter);//配置适配器

                    shopSaleSlidingTab.setViewPager(shopSaleVp);//关联适配器
                } else {
                    showToast(shopSaleData.getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
            }
        });
    }

    @Override
    protected void initView() {
        shopSaleSlidingTab.setOnTabSelectListener(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            type = bundle.getInt("type", -1);
            title = bundle.getString("title");

            if (type == Constant.NUM_1) {
                brandId = -1;
                content = bundle.getString("content");
            } else if (type == Constant.NUM_2 || type == Constant.NUM_3) {
                brandId = -1;
                content = "";
            } else if (type == Constant.NUM_4) {
                brandId = bundle.getInt("brandId", -1);
                content = "";
            }
        }

        setTitle(title);
    }

    /**
     * tab 选中
     *
     * @param position
     */
    @Override
    public void onTabSelect(int position) {
        shopSaleSlidingTab.setCurrentTab(position);
        shopSaleSlidingTab.setCurrentTab(position, false);
    }

    @Override
    public void onTabReselect(int position) {

    }

    @Override
    @OnClick({R.id.shop_cart_iv})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.shop_cart_iv:
                startActivity(CartActivity.class);
                break;
        }
    }
}
